package org.kongqian.orgs.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.kongqian.basic.utils.AjaxResult;
import org.kongqian.basic.utils.PageList;
import org.kongqian.orgs.domain.Employee;
import org.kongqian.orgs.query.EmployeeQuery;
import org.kongqian.orgs.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理Controller",description = "员工管理Controller")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PutMapping
    @ApiOperation(value = "员工保存")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId()!=null){
                employeeService.updateById(employee);
            }else {
                employeeService.insert(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
        return AjaxResult.getResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除")
    public AjaxResult delete(@ApiParam(required = true) @PathVariable("id") Long id){
        try {
            employeeService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
        return AjaxResult.getResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个员工")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return AjaxResult.getResult().setObject(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "部门分页和高级查询")
    public AjaxResult listPage(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> departmentPageList = employeeService.listPage(query);
            return AjaxResult.getResult().setObject(departmentPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询失败");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询部门领导")
    public AjaxResult selectAll(){
        try {
            List<Employee> employee=employeeService.selectAll();
            return AjaxResult.getResult().setObject(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询部门领导错误！");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    public AjaxResult batchDeleteDept(@RequestBody List<Long> ids){
        try {
            employeeService.batchDeleteDept(ids);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("批量删除部门失败");
        }
    }

}
